<?php

namespace App\Http\Controllers\Order;

use App\Http\Controllers\Controller;
use App\Models\Order;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\Models\Elsearch;

class OrderController extends Controller
{
    public $_model_order = null;
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->_model_order = new Order();
    }

    public function dcs_keyword_search(Request $request){
        $get = $request->all();
        $rs = array();
        if(isset($get['term'])){
            $Elasticsearch = new Elsearch();
            $result = $Elasticsearch->search_dangerous($get['term'],0,15);
            if($result){
                foreach ($result['hits'] as $key => $value) {
                    $rs[] = array('value'=>$value['_source']['index_id'],'label'=>$value['_source']['name']);
                }
            }
        }
        echo json_encode($rs);
    }
    public function uploadContract(Request $request){
        $get = $request->all();
        $rs = array();
        $rs['material'] = $this->_model_order->get_order_material_by_order_id($get['order_id']);
        $rs['contract_order_id'] = $get['order_id'];
        return view('order/uploadContract', ['rs'=>$rs]);
    }
    public function uploadMaterial(Request $request){
        $get = $request->all();

        $rs = array();
        if ($request->isMethod('post')) {
            $rs['order_id'] = $get['order_id'];

            $pic = $request->file('license');
            if($pic){
                $name = $pic->getClientOriginalName();//得到图片名；
                $path = date('Y-m');
                $fileName = rand(1,999).'_'.$name;//生成新的的文件名
                Storage::disk('material')->makeDirectory($path);
                $filePath = $path.'/'.$fileName;
                Storage::disk('material')->put($filePath, file_get_contents($pic->getRealPath()));
                $rs['license'] = $filePath;
            }
            
            $pic = $request->file('legal_card');
            if($pic){
                $name = $pic->getClientOriginalName();//得到图片名；
                $path = date('Y-m');
                $fileName = rand(1,999).'_'.$name;//生成新的的文件名
                Storage::disk('material')->makeDirectory($path);
                $filePath = $path.'/'.$fileName;
                Storage::disk('material')->put($filePath, file_get_contents($pic->getRealPath()));
                $rs['legal_card'] = $filePath;
            }

            $pic = $request->file('market_card');
            if($pic){
                $name = $pic->getClientOriginalName();//得到图片名；
                $path = date('Y-m');
                $fileName = rand(1,999).'_'.$name;//生成新的的文件名
                Storage::disk('material')->makeDirectory($path);
                $filePath = $path.'/'.$fileName;
                Storage::disk('material')->put($filePath, file_get_contents($pic->getRealPath()));
                $rs['market_card'] = $filePath;
            }

            $pic = $request->file('shop_card');
            if($pic){
                $name = $pic->getClientOriginalName();//得到图片名；
                $path = date('Y-m');
                $fileName = rand(1,999).'_'.$name;//生成新的的文件名
                Storage::disk('material')->makeDirectory($path);
                $filePath = $path.'/'.$fileName;
                Storage::disk('material')->put($filePath, file_get_contents($pic->getRealPath()));
                $rs['shop_card'] = $filePath;
            }

            $pic = $request->file('transport_proxy');
            if($pic){
                $name = $pic->getClientOriginalName();//得到图片名；
                $path = date('Y-m');
                $fileName = rand(1,999).'_'.$name;//生成新的的文件名
                Storage::disk('material')->makeDirectory($path);
                $filePath = $path.'/'.$fileName;
                Storage::disk('material')->put($filePath, file_get_contents($pic->getRealPath()));
                $rs['transport_proxy'] = $filePath;
            }

            $pic = $request->file('contract');
            if($pic){
                $name = $pic->getClientOriginalName();//得到图片名；
                $path = date('Y-m');
                $fileName = rand(1,999).'_'.$name;//生成新的的文件名
                Storage::disk('contract')->makeDirectory($path);
                $filePath = $path.'/'.$fileName;
                Storage::disk('contract')->put($filePath, file_get_contents($pic->getRealPath()));
                $rs['contract'] = $filePath;
            }

            $pics = $request->file('dcs_card');
            if($pics){
                foreach ($pics as $key => $pic) {
                    $name = $pic->getClientOriginalName();//得到图片名；
                    $path = date('Y-m');
                    $fileName = rand(1,999).'_'.$name;//生成新的的文件名
                    Storage::disk('material')->makeDirectory($path);
                    $filePath = $path.'/'.$fileName;
                    Storage::disk('material')->put($filePath, file_get_contents($pic->getRealPath()));
                    $rs['dcs_card'][] = $filePath;
                }
                $rs['dcs_card'] = implode(',', $rs['dcs_card']);
            }

            $pics = $request->file('dcs_card_attach');
            if($pics){
                foreach ($pics as $key => $pic) {
                    $name = $pic->getClientOriginalName();//得到图片名；
                    $path = date('Y-m');
                    $fileName = rand(1,999).'_'.$name;//生成新的的文件名
                    Storage::disk('material')->makeDirectory($path);
                    $filePath = $path.'/'.$fileName;
                    Storage::disk('material')->put($filePath, file_get_contents($pic->getRealPath()));
                    $rs['dcs_card_attach'][] = $filePath;
                }
                $rs['dcs_card_attach'] = implode(',', $rs['dcs_card_attach']);
            }

            $pic = $request->file('buy_card');
            if($pic){
                $name = $pic->getClientOriginalName();//得到图片名；
                $path = date('Y-m');
                $fileName = rand(1,999).'_'.$name;//生成新的的文件名
                Storage::disk('material')->makeDirectory($path);
                $filePath = $path.'/'.$fileName;
                Storage::disk('material')->put($filePath, file_get_contents($pic->getRealPath()));
                $rs['buy_card'] = $filePath;
            }

            $pic = $request->file('illustrate');
            if($pic){
                $name = $pic->getClientOriginalName();//得到图片名；
                $path = date('Y-m');
                $fileName = rand(1,999).'_'.$name;//生成新的的文件名
                Storage::disk('material')->makeDirectory($path);
                $filePath = $path.'/'.$fileName;
                Storage::disk('material')->put($filePath, file_get_contents($pic->getRealPath()));
                $rs['illustrate'] = $filePath;
            }

            $pic = $request->file('bussines_legal_card');
            if($pic){
                $name = $pic->getClientOriginalName();//得到图片名；
                $path = date('Y-m');
                $fileName = rand(1,999).'_'.$name;//生成新的的文件名
                Storage::disk('material')->makeDirectory($path);
                $filePath = $path.'/'.$fileName;
                Storage::disk('material')->put($filePath, file_get_contents($pic->getRealPath()));
                $rs['bussines_legal_card'] = $filePath;
            }

            $this->_model_order->update_cust_material($rs);
            $rs['reload'] = 1;
            return view('order/uploadMaterial', ['rs'=>$rs]);
        }
        if ($request->isMethod('get')) {
            $rs['order_detail'] = $this->_model_order->get_order_info_by_order_id($get['id']);
            $rs['order_basic'] = $this->_model_order->get_order_basic_by_id($rs['order_detail']->order_basic_id);
            $rs['material'] = $this->_model_order->get_cust_material($rs['order_basic']->cust_id);
            $shop_card_id = $this->_model_order->get_order_material_by_order_list_id($rs['order_detail']->id);
            if($shop_card_id->shop_card_id){
                $rs['shop_card'] = $this->_model_order->get_shop_card_by_id($shop_card_id->shop_card_id);
            }
            // print_rf($rs);die();
            return view('order/uploadMaterial', ['rs'=>$rs]);
        }
        
    }

    public function detail(Request $request){
        $get = $request->all();
        $rs = array();
        $rs['order_detail'] = $this->_model_order->get_order_info_by_order_id($get['id']);
        $rs['order_basic'] = $this->_model_order->get_order_basic_by_id($rs['order_detail']->order_basic_id);
        $rs['order_address'] = $this->_model_order->getAddrById($rs['order_basic']->address_id);
        $rs['product'] = current($this->_model_order->get_order_product_by_order_ids(array($rs['order_detail']->id)));
        $rs['cust_material'] = $this->_model_order->get_cust_material($rs['order_basic']->cust_id);
        $rs['order_material'] = $this->_model_order->get_order_material_by_order_list_id($rs['order_detail']->id);
        $rs['shop_card'] = array();
        if($rs['order_detail']->shop_card_status > 0){
            if($rs['order_material']->shop_card_id){
                $rs['shop_card'] = $this->_model_order->get_shop_card_by_id($rs['order_material']->shop_card_id);
            }
        }
        // print_rf($rs);die();
        return view('order/detail', ['rs'=>$rs]);
    }
    public function ajaxGetProductPriceList(Request $request){
        $rs = array();
        $get = $request->all();
        $query = array();
        $query['count'] = true;
        $query['page'] = $get['page'];
        $query['size'] = 10;
        $query['status'] = 1;
        $query['is_default'] = 1;
        $query['store_count'] = true;
        $query['danger_type'] = true;
        if(isset($get['key'])){
            $query['name'] = trim($get['key']);
        }
        $total = $this->_model_order->get_product_pirce_list($query);
        unset($query['count']);
        $rs['data'] = $this->_model_order->get_product_pirce_list($query);
        $rs['pages'] = ceil($total/$query['size']);
        return $rs;
    }
    public function ajaxCancelContract(Request $request){
        $get = $request->all();
        $order_info = $this->_model_order->get_order_info_by_order_id($get['order_id']);
        if(isset($get['contract_type'])){
            $this->_model_order->upload_order_material($get,$order_info->id);
        }
        $this->_model_order->update_order_ignore_contract($get['order_id']);
        $order_info->status = 10;
        $this->_model_order->after_upload_contract($order_info);
        echo 1;
    }
    public function ajaxUploadContract(Request $request){
        $post = $request->all();
        // print_r($post);die();
        $order_info = $this->_model_order->get_order_info_by_order_id($post['order_id']);
        $property = $order_info->property;
        $contract_type = $post['contract_type'];
        $this->_model_order->upload_order_material($post,$order_info->id);
        $status = 0;
        switch ($contract_type) {
            case '0'://0标准无价易制毒合同
                switch ($property) {
                    case 'normal':
                        $status = 40;//待支付
                    break;
                    case 'poison':
                        $status = 10;//待上传
                    break;
                    case 'detonate':
                        $status = 10;//待上传
                    break;
                }
                break;
            case '1'://1标准有价易制毒合同
                switch ($property) {
                    case 'normal':
                        $status = 40;//待支付
                    break;
                    case 'poison':
                        
                    break;
                    case 'detonate':
                        
                    break;
                }
                break;
            case '2'://2非标准无价易制毒合同
                switch ($property) {
                    case 'normal':
                        $status = 40;//待支付
                    break;
                    case 'poison':
                        
                    break;
                    case 'detonate':
                        
                    break;
                }
                break;
            case '3'://3非标准有价易制毒合同
                switch ($property) {
                    case 'normal':
                        $status = 40;//待支付
                    break;
                    case 'poison':
                        
                    break;
                    case 'detonate':
                        
                    break;
                }
                break;
            case '4'://4单笔标准合同
                switch ($property) {
                    case 'normal':
                        $status = 40;//待支付
                    break;
                    case 'poison':
                        
                    break;
                    case 'detonate':
                        
                    break;
                }
                break;
            case '5'://5单笔非标准合同
                switch ($property) {
                    case 'normal':
                        $status = 40;//待支付
                    break;
                    case 'poison':
                        
                    break;
                    case 'detonate':
                        
                    break;
                }
                break;
            case '6'://6单笔标准合同含附件条件
                if($post['contract']){

                }else{

                }
            default:
                break;
        }
        $this->_model_order->update_order_list_status($status,$post['order_id']);
        $order_info->status = $status;
        $this->_model_order->after_upload_contract($order_info);
        echo 1;
    }

    public function create(){
        $rs = array();
        return view('order/create', ['rs'=>$rs]);
    }

    public function createOrderSuccess(Request $request){
        $get = $request->all();
        $rs = array();
        $order_list = $this->_model_order->get_list_by_basic_id($get['basic_id']);
        if($order_list){
            foreach ($order_list as $key => $value) {
                $order_ids[] = $value->id;
            }
            $order_product = $this->_model_order->get_order_product_by_order_ids($order_ids);
            foreach ($order_list as $key => $value) {
                $order_list[$key]->product_list = $order_product[$value->id];
            }
        }
        $rs['order_list'] = $order_list;
        return view('order/success', ['rs'=>$rs]);
    }

    public function ajax_upload_contract_file(Request $request){
        //{{resizeImage('contract/2018-08-02/386_1-1.png','90','100')}}
        $rs = array();
        // ini_set('memory_limit', '256M');
        $data = $request->input();
        $pic = $request->file('file_data');
        $name = $pic->getClientOriginalName();//得到图片名；
        // $ext = $pic->getClientOriginalExtension();//得到图片后缀；
        $path = date('Y-m-d');
        $fileName = rand(1,999).'_'.$name;//生成新的的文件名
        Storage::disk('contract')->makeDirectory($path);
        $filePath = $path.'/'.$fileName;
        Storage::disk('contract')->put($filePath, file_get_contents($pic->getRealPath()));
        
        $rs['small']  = resizeImage('contract/'.$filePath,'100','100');
        $rs['big']  = resizeImage('contract/'.$filePath,'640','640');
        $rs['v'] = $filePath;
        echo json_encode($rs);
    }

    public function ajax_upload_material_file(Request $request){
        $rs = array();
        $data = $request->input();
        foreach ($request->file() as $key => $value) {
            foreach ($value as $k => $pic) {
                $name = $pic->getClientOriginalName();//得到图片名；
                // $ext = $pic->getClientOriginalExtension();//得到图片后缀；
                $path = date('Y-m-d');
                $fileName = rand(1,999).'_'.$name;//生成新的的文件名
                Storage::disk('material')->makeDirectory($path);
                $filePath = $path.'/'.$fileName;
                Storage::disk('material')->put($filePath, file_get_contents($pic->getRealPath()));

                // $rs['small']  = resizeImage('material/'.$filePath,'100','100');
                // $rs['big']  = resizeImage('material/'.$filePath,'640','640');
                $rs['v'][] = $filePath;
            }
         }
        echo json_encode($rs);
    }

    public function downpic(Request $request){
        $get = $request->all();
        $exists = Storage::disk($get['type'])->exists($get['id']);
        $pic_name = str_replace('/', '-', $get['id']);
        return response()->download(storage_path('app/public/contract/').$get['id'],
            $pic_name);
    }

    public function orderList(Request $request){
        $get = $request->all();
        // print_r($get);die();
        $rs = array();
        $rs['list'] = $this->_model_order->get_order_list($get);
        $rs['list']->appends($get);
        $rs['size'] = $rs['list']->total();
        $rs['page'] = $rs['list']->links();
        $rs['list'] = $rs['list']->toArray()['data'];
        $rs['statistics'] = $this->_model_order->statistics_order_manage();
        // print_rf($rs['page']->toHtml());die();
        return view('order/list', ['rs'=>$rs]);
    }
    public function createpost(Request $request){
        $post = $request->all();
        if($post['product_obj']){
            foreach ($post['product_obj'] as $key => $value) {
                 parse_str($value,$post['product_obj'][$key]);
            }
        }
        $order_basic_id = $this->_model_order->create_order_basic($post);
        echo json_encode(array('id'=>$order_basic_id));
        exit();
        //拆单
        if($post['product_obj']){
            $order_list = array();
            foreach ($post['product_obj'] as $key => $value) {
                if(isset($post['num'][$value['stock_num']])){
                    $value['num'] = $post['num'][$value['stock_num']];
                }else{
                    die('error num');
                }
                if(isset($post['price'][$value['stock_num']])){
                    $value['price'] = $post['price'][$value['stock_num']];
                }else{
                    die('error price');
                }

                $value = $this->split_size($value);

                $value['product_id'] = $this->_model_order->create_product($value);
                $post['product_obj'][$key]['property'] = $this->_model_order->process_transl_property($value['property']);
                
                $order_list[$post['product_obj'][$key]['property']][] = $value;
            }
            if(isset($order_list)){
                foreach ($order_list as $key => $value) {
                    if($key == 'poison'){   //易制毒
                        foreach ($value as $k => $v) {
                            $insert_order = array();
                            $insert_order['order_id'] = $this->_model_order->create_order_num();
                            $insert_order['property'] = $key;
                            $insert_order['order_basic_id'] = $order_basic_id;
                            $insert_order['product_ids'] = $v['product_id'];
                            $insert_order['order_product'][] = $v;
                            $insert_order['total_price'] = $v['price']*$v['num'];
                            $this->_model_order->create_order($insert_order);
                        }
                    }else{
                        $total_price = 0;
                        $insert_order = array();
                        $insert_order['order_id'] = $this->_model_order->create_order_num();
                        $insert_order['property'] = $key;
                        $insert_order['order_basic_id'] = $order_basic_id;
                        foreach ($value as $k => $v) {
                            $insert_order['product_ids'][] = $v['product_id'];
                            $insert_order['order_product'][] = $v;
                            $total_price += $v['price']*$v['num'];
                        }
                        $insert_order['total_price'] = $total_price;
                        $insert_order['product_ids'] = implode(',', $insert_order['product_ids']);
                        $this->_model_order->create_order($insert_order);
                    }
                }
                echo json_encode(array('id'=>$order_basic_id));
            }
        }
    }

    public function ajaxCreateAddr(Request $request){
        $post = $request->all();
        if(!$post['area_id']){
            echo 0;
            exit();
        }
        $tmp = explode(',', $post['area_id']);
        if(count($tmp) != 3){
            echo 0;
            exit();
        }
        $post['city_id'] = $tmp[0];
        $post['province_id'] = $tmp[1];
        $post['area_id'] = $tmp[2];
        echo $this->_model_order->create_address($post);
    }
    public function ajaxgetAddrByCust(Request $request){
        $get = $request->all();
        if(isset($get['cust_id'])){
            $rs = $this->_model_order->getAddrByCustID($get['cust_id'],array('id','address','detail'));
            echo $rs->toJson();
        }
    }
}
